import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class OperationListener implements ActionListener {

    Register register;
    AnswerLabel answer;
    char sign;

    void setRegister(Register r)
    {
        register = r;
    }

    Register getRegister() {
        return register;
    }

    void setAnswer(AnswerLabel a) {
        answer = a;
    }

    AnswerLabel getAnswer() {
        return answer;
    }

    void setSign(char c) {
        sign = c;
    }

    char getSign() {
        return sign;
    }

    public OperationListener(Register r, AnswerLabel a, char c)
    {
        setRegister(r);
        setAnswer(a);
        setSign(c);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(answer.getStatus() != AnswerLabel.ABORT) {
            if (register.getOperation() != '\0') register.calculate(Double.parseDouble(answer.getText()));
            else register.setRes(Double.parseDouble(answer.getText()));
            answer.loadDouble(register.getRes());
            answer.setStatus(AnswerLabel.ABORT);
        }
        register.setOperation(sign);
        register.updateText();
    }
}
